
const express = require('express')
const router = express.Router()
const Article = require('../../models/Article')
const Category = require('../../models/Category')

let responseData = {
	resultCode: 0,
	resultMsg: 'success',
	data: {}
}

/**
 * 后台查询博客列表接口  /article
 */
router.get('/', function(req, res) {
	Article.find().then(function(articleList) {
		responseData.data = articleList
		responseData.resultCode = 0
		responseData.resultMsg = 'success'
		res.json(responseData)
		return
	})
})


/**
 * 后台查询某一条博客接口  /article/detail
 */
router.get('/detail', function(req, res) {
	console.log(req.query)
	if(JSON.stringify(req.query) == '{}') {
		responseData.resultCode = 1
		responseData.resultMsg = '请传入参数'
		res.json(responseData)
		return
	}
	Article.findOne({...req.query}).then(function(articleInfo) {
		if(articleInfo) {
			responseData.resultCode = 0
			responseData.resultMsg = 'success'
			responseData.data = articleInfo
			res.json(responseData)
			return
		}else {
			responseData.resultCode = 1
			responseData.resultMsg = '不存在这条数据'
			res.json(responseData)
		}
	}, function(err) {
		console.log(err)
	})
})



/**
 * 后台添加博客接口  /article/add
 * 参数： articleTitle, articleTime, articleTag, articleContent 
 */
router.post('/add', function(req, res) {
	let { articleTitle, articleTime, articleTag, articleContent, articleDesc } = req.body
	articleTag = articleTag.split(',')
	// 判断标题是否已经存在，如果存在，不应该再添加
	Article.findOne({
		articleTitle	
	}).then((articleInfo) => {
		if(articleInfo) {
			responseData.resultCode = 1
			responseData.resultMsg = '文章标题已经存在'
			res.json(responseData)
			return
		}
	})
	console.log(JSON.stringify(articleTag), '---------')
	let article = new Article({
		articleTitle,
		articleTime,
		articleTag,
		articleContent,
		articleDesc
	})

	article.save().then(function(articleInfo) {
		responseData.resultCode = 0
		responseData.resultMsg = 'success'
		res.json(responseData)
		res.end()
	}, function(err) {
		console.log(err)
		responseData.resultCode = 1
		responseData.resultMsg = err.message
		res.json(responseData)
	})
})



/**
 * 后台更新博客内容接口  /article/update
 */
router.post('/update', function(req, res) {
	let { id, articleTitle, articleTime, articleTag, articleContent, articleDesc } = req.body
	console.log(articleTag, '---')
	if(!id) {
		responseData.resultCode = 1
		responseData.resultMsg = '参数错误',
		res.json(responseData)
	}
	Article.findOne({
		articleTitle: articleTitle
	}).then(function(articleInfo) {
		if(articleInfo) {
			if(articleInfo._id + '' !== id) {
				console.log('不相等')
				responseData.resultCode = 1
				responseData.resultMsg = '博客标题已存在'
				res.json(responseData)
				return
			}
		}
		articleTag = articleTag.split(',')
		Article.update({
			_id: id
		}, {
			articleTitle: articleTitle,
			articleTime: articleTime,
			articleTag: articleTag,
			articleContent: articleContent,
			articleDesc: articleDesc
		}).then(function(articleInfo) {
			res.json(responseData)
		}, function(err) {
			responseData.resultCode = 1
			responseData.resultMsg = err.message
			res.json(responseData)
		})
	})
})


/** 后台删除博客接口 /article/delete
 *  需要数据： id 
 */
router.get('/delete', function(req, res){
	let id = req.query.id
	Article.findOne({
		_id: id
	}).then(function(articleInfo) {
		if(!articleInfo) {
			responseData.resultCode = 1
			responseData.resultMsg = '博客不存在'
			res.json(responseData)
			return 
		}
		Article.remove({
			_id: id
		}).then(function(affectLine) {
			responseData.resultCode = 0
			responseData.resultMsg = 'success'
			res.json(responseData)
		})
	})
})


// 查询全部文章类目信息
router.get('/tree', function(req, res) {
	console.log('111')
	Category.find().then((tagList) => {
		let result = []
		let promises = []
		tagList.forEach((item, index) => {
			promises.push(new Promise((resolve, reject) => {
				Article.find({articleTag: item.categoryName}).then(articleList => {
					let tempList = JSON.parse(JSON.stringify(articleList))
					tempList.forEach(item => {
						item.title = item.articleTitle
						item.key = item.articleTitle
					})
					result.push({title: item.categoryName, key: item.categoryName, children: tempList})
					resolve()
				})
			}))
		})
		Promise.all(promises).then(() => {
			responseData.resultCode = 0
			responseData.resultMsg = 'success'
			responseData.data = result
			res.json(responseData)
		})
	})
})


module.exports = router